import java.util.Stack;

/*
 * @lc app=leetcode.cn id=232 lang=java
 *
 * [232] 用栈实现队列
 */

// @lc code=start
class MyQueue {
    // 加一个辅助栈来操作，就是比较费空间。
    // 妈的，优化了一下一样的内存消耗，算了，差不多得了。
    private Stack<Integer> s;
    private Stack<Integer> h;//辅助栈
    /** Initialize your data structure here. */
    public MyQueue() {
        s = new Stack<>();
        h = new Stack<>();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        s.push(x);//不变
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        //h空，则全pop到辅助栈，若不空，则直接从辅助栈返回
        if(h.isEmpty()){
            while(!s.empty()){
                h.push(s.pop());
            }
        }
        
        return h.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        //同pop的原理，只是不用pop了
        if(h.isEmpty()){
            while(!s.empty()){
                h.push(s.pop());
            }
        }
        
        return h.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return s.empty()&&h.empty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
// @lc code=end

